package midlets;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import org.ksoap.SoapObject;
import org.ksoap.transport.HttpTransport;

import util.Criptografia;

public class Teste extends MIDlet  implements CommandListener{
	
	private final String URL = "http://localhost:80/webservice_SCGP/webServiceSCGP.php";
	private final String CODIGO_WEBSERVICE = "19287bhcby";
	private final String FUNCAO_SEND_BACKUP_PERIODO = "sendBackupPeriodo";
	
	
	public static Display display;
	public static Alert alert1;
	public static Command c_sair;
	
	
	
	protected void startApp() throws MIDletStateChangeException {

		String x = new Criptografia().criptografarString("Descricao boa cara");
		
		System.out.println(x);
		System.out.println(new Criptografia().criptografarString("Descricao boa cara"));
		System.out.println(new Criptografia().descriptografarString(x));
		
		display = Display.getDisplay(this);
		c_sair = new Command("Sair", Command.EXIT ,1);
		alert1 = new Alert("","" , null, AlertType.INFO);
		alert1.setTimeout(Alert.FOREVER);
		alert1.addCommand(c_sair);
		alert1.setCommandListener(this);
		display.setCurrent(alert1);
		
		
		/*
		try {

			SoapObject client = new SoapObject(URL, FUNCAO_SEND_BACKUP_PERIODO);
			client.addProperty("dados", "1|2010|1#2|2010|2#3|2010|3");
			client.addProperty("codigoacesso", CODIGO_WEBSERVICE);

			HttpTransport ht = new HttpTransport(URL, FUNCAO_SEND_BACKUP_PERIODO);

			String retornoDados = (String) ht.call(client);

			alert1.setString(retornoDados);
			display.setCurrent(alert1);
			System.out.println(retornoDados);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		*/
	}
	
	
	
	

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
		
	}





	public void commandAction(Command c, Displayable d) {
		
		if (c == c_sair) {
			
			/*
			try {
				destroyApp(true);
			} catch (MIDletStateChangeException e) {
				e.printStackTrace();
			}
			notifyDestroyed();
			*/
			
			try {

				SoapObject client = new SoapObject(URL, FUNCAO_SEND_BACKUP_PERIODO);
				client.addProperty("dados", "1|2010|1#2|2010|2");
				client.addProperty("codigoacesso", CODIGO_WEBSERVICE);

				HttpTransport ht = new HttpTransport(URL, FUNCAO_SEND_BACKUP_PERIODO);

				String retornoDados = (String) ht.call(client);

				alert1.setString(retornoDados);
				display.setCurrent(alert1);
				System.out.println(retornoDados);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		} 
	}

}
